package btInquiryRssi;
/**
 * Questo ha il compito di scrivere i mac ed i relativi valori RSSI dei client
 * rilevati nel file inquiry.txt.
 */

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

public class InquiryThread extends Thread {
	IBluetooth iBluetooth;
	FileOutputStream out = null; 
	PrintStream ps; 
	int time;
	int devices;
	String[] mac_rssi_device = null;
	int thresholding;

	public InquiryThread(int inquiryTime,int devicesNumber,int thresholding,IBluetooth iBluetooth) {
		this.time= inquiryTime;
		this.devices= devicesNumber;
		this.iBluetooth= iBluetooth;
		this.thresholding = thresholding;
	}

	public void run(){	

		System.err.println("-> inquiryThread avviato con maxTime = "+time+", maxPacket = "+devices+", RssiThresholding = "+thresholding);

		mac_rssi_device = iBluetooth.inquiry(time,devices);	
		if(mac_rssi_device!=null){
			try {
				out = new FileOutputStream("./gatewayTmp/inquiryTmp.txt");
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			ps = new PrintStream(out);
			for(int i=0;i<mac_rssi_device.length;i++){
				String client = mac_rssi_device[i];
				String mac = client.replaceAll(":","").substring(0,12);
				String rssi = client.substring(18);
				//if(Integer.parseInt(rssi) < thresholding){
				ps.println(":"+mac+":"+rssi+":");
				//}
			}	
			ps.close();
		}

		System.err.println("inquiryThread: termino!");
	}//fine run	
}


